<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1 , minimum-scale=1.0,maximum-scale =1.0,user-scalable=no">


<title>添加信用卡</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="../css/addBankCard.css">
<script type="text/javascript" src="../js/jquery-1.8.3.js"></script>
<script type="text/javascript" src="../js/layer/layer.js"></script>
<link rel="stylesheet" href="../css/layer.css" id="layuicss-skinlayercss">
<script type="text/javascript">
	//标记>>姓名>>身份证>>>预留手机号>>>银行卡号
	var openID;
	$(function() {
		$('#id_name').val(localStorage.getItem("ll_id_name"));
		openID = localStorage.getItem("ll_openID");
	});

	//确认提交
	function submit() {
		var id_name = $('#id_name').val();//持卡人
		var id_no = $('#id_no').val();//身份证
		var bank_phone = $("#bank_phone").val(); //银行预留手机号
		var card_no = $('#card_no').val();//卡号
		
		var reg = /^1[3|4|5|7|8][0-9]{9}$/; //手机号验证规则  
		//判断
		if (id_name == "" || id_no == "" || bank_phone == "" || card_no == "") {
			layer.msg("所有信息必须填写完整！");
			return;
		}
		
		if (!reg.test(bank_phone.trim())) {
			layer.msg("手机号验证失败，请核对后再提交！");
			return;
		}
		
		var isCard = isCardID(id_no);
		if(isCardID(id_no) != true){
			layer.msg(isCard);
			return;
		}
		
		var bankCheck = luhmCheck(card_no);
		if(bankCheck == false){
			layer.msg("银行卡验证失败,请核对后再提交！");
			return;
		}
		
		$.ajax({
			url : "../llPay/addCredit",
			type : 'post',
			data : {
				'openID' : openID,
				'id_name' : id_name,
				'id_no' : id_no,
				'card_no' : card_no,
				'bank_phone' : bank_phone
			},
			success : function(message) {
				if (message == 'success') {
					layer.msg('添加成功');
					window.location.href = "xuanzezhifukapian.html";
				} else if (message == "repeat") {
					layer.msg('您已绑定该银行卡，请勿重复绑定');
				} else {
					layer.msg('提交失败，请稍后再试！');
				}
			},
			error : function(){
				layer.msg("提交失败，请稍后再试！");
			}
		});
	};
	
	var aCity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",  
	        21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",  
	        33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",  
	        42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",  
	        51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",  
	        63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"  
	}; 
	function isCardID(sId){
		 var iSum=0 ;
		 var info="" ;
		 if(!/^\d{17}(\d|x)$/i.test(sId)){
			 return "你输入的身份证长度或格式错误";
		 } 
		 sId=sId.replace(/x$/i,"a");
		 if(aCity[parseInt(sId.substr(0,2))]==null){
			 return "你的身份证地区非法";
		 }
		 sBirthday=sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2));
		 var d=new Date(sBirthday.replace(/-/g,"/")) ;
		 if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate())){
			 return "身份证上的出生日期非法";
		 }
		 for(var i = 17;i>=0;i --) iSum += (Math.pow(2,i) % 11) * parseInt(sId.charAt(17 - i),11) ;
		 if(iSum%11!=1){
			 return "你输入的身份证号非法";
		 }
		 return true;
	}
	
	/* 获取url后的字符串 */
	function GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			return (r[2]);
		}
		return null;
	}

	//bankno为银行卡号 banknoInfo为显示提示信息的DIV或其他控件  
	function luhmCheck(bankno){  
	    var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）  
	   
	    var first15Num=bankno.substr(0,bankno.length-1);//前15或18位  
	    var newArr=new Array();  
	    for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组  
	        newArr.push(first15Num.substr(i,1));  
	    }  
	    var arrJiShu=new Array();  //奇数位*2的积 <9  
	    var arrJiShu2=new Array(); //奇数位*2的积 >9  
	       
	    var arrOuShu=new Array();  //偶数位数组  
	    for(var j=0;j<newArr.length;j++){  
	        if((j+1)%2==1){//奇数位  
	            if(parseInt(newArr[j])*2<9)  
	            arrJiShu.push(parseInt(newArr[j])*2);  
	            else  
	            arrJiShu2.push(parseInt(newArr[j])*2);  
	        }  
	        else //偶数位  
	        arrOuShu.push(newArr[j]);  
	    }  
	       
	    var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数  
	    var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数  
	    for(var h=0;h<arrJiShu2.length;h++){  
	        jishu_child1.push(parseInt(arrJiShu2[h])%10);  
	        jishu_child2.push(parseInt(arrJiShu2[h])/10);  
	    }          
	       
	    var sumJiShu=0; //奇数位*2 < 9 的数组之和  
	    var sumOuShu=0; //偶数位数组之和  
	    var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和  
	    var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和  
	    var sumTotal=0;  
	    for(var m=0;m<arrJiShu.length;m++){  
	        sumJiShu=sumJiShu+parseInt(arrJiShu[m]);  
	    }  
	       
	    for(var n=0;n<arrOuShu.length;n++){  
	        sumOuShu=sumOuShu+parseInt(arrOuShu[n]);  
	    }  
	       
	    for(var p=0;p<jishu_child1.length;p++){  
	        sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);  
	        sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);  
	    }        
	    //计算总和  
	    sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);  
	       
	    //计算Luhm值  
	    var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;          
	    var luhm= 10-k;  
	       
	    if(lastNum==luhm && lastNum.length != 0){  
	       //$("#banknoInfo").html("Luhm验证通过");  
	       return true;  
	    }  
	    else{  
	       //$("#banknoInfo").html("银行卡号必须符合Luhm校验");  
	       return false;  
	    }          
	}  
</script>
</head>
<body>
	<div id="all">
		<div id="head"></div>
		<div id="center">
			<div id="kuan">
				<div class="detail-top">
					<div class="detail-real">姓名</div>
					<div class="detail-input">
						<input type="text" placeholder="请填写姓名" id="id_name" >
					</div>
				</div>
				<div class="detail-top">
					<div class="detail-real">身份证</div>
					<div class="detail-input">
						<input type="text" placeholder="请填写身份证号码" id="id_no" >
					</div>
				</div>
				<div class="detail-top">
					<div class="detail-real">卡号</div>
					<div class="detail-input">
						<input type="text" placeholder="请填写银行卡号" id="card_no">
					</div>
				</div>
				<div class="detail-top">
					<div class="detail-real  ">手机号</div>
					<div class="detail-input">
						<input type="text" placeholder="请填写银行预留手机号" id="bank_phone">
					</div>
				</div>
			</div>
		</div>
		<div class="out-side" style="text-align: center; margin-top: 40px">
			<div class="updown">
				<img src="../img/btn.png">
			</div>
			<div class="anzhuan" onclick="submit()">确认提交</div>
		</div>
	</div>



</body>
</html>